// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.vehicles;

import java.util.ArrayList;
import java.util.Arrays;
import zombie.core.Core;
import zombie.debug.DebugLog;
import zombie.network.GameClient;

public final class VehicleIDMap {
    public static final VehicleIDMap instance = new VehicleIDMap();
    private static final int MAX_IDS = 32767;
    private static final int RESIZE_COUNT = 256;
    private int capacity = 256;
    private BaseVehicle[] idToVehicle;
    private short[] freeID;
    private short freeIDSize;
    private boolean noise = false;
    private int warnCount = 0;

    VehicleIDMap() {
        this.idToVehicle = new BaseVehicle[this.capacity];
        this.freeID = new short[this.capacity];

        for (int _int = 0; _int < this.capacity; _int++) {
            this.freeID[this.freeIDSize++] = (short)_int;
        }
    }

    public void put(short id, BaseVehicle vehicle) {
        if (Core.bDebug && this.noise) {
            DebugLog.log("VehicleIDMap.put()" + id);
        }

        if (GameClient.bClient && id >= this.capacity) {
            this.resize((id / 256 + 1) * 256);
        }

        if (id < 0 || id >= this.capacity) {
            throw new IllegalArgumentException("invalid vehicle id " + id + " max=" + this.capacity);
        } else if (this.idToVehicle[id] != null) {
            throw new IllegalArgumentException("duplicate vehicle with id " + id);
        } else if (vehicle == null) {
            throw new IllegalArgumentException("vehicle is null");
        } else {
            this.idToVehicle[id] = vehicle;
        }
    }

    public void remove(short id) {
        if (Core.bDebug && this.noise) {
            DebugLog.log("VehicleIDMap.remove()" + id);
        }

        if (id < 0 || id >= this.capacity) {
            throw new IllegalArgumentException("invalid vehicle id=" + id + " max=" + this.capacity);
        } else if (this.idToVehicle[id] == null) {
            throw new IllegalArgumentException("no vehicle with id " + id);
        } else {
            this.idToVehicle[id] = null;
            if (!GameClient.bClient && !zombie.GameWindow.bLoadedAsClient) {
                this.freeID[this.freeIDSize++] = id;
            }
        }
    }

    public BaseVehicle get(short id) {
        return id >= 0 && id < this.capacity ? this.idToVehicle[id] : null;
    }

    public boolean containsKey(short id) {
        return id >= 0 && id < this.capacity && this.idToVehicle[id] != null;
    }

    public void toArrayList(ArrayList<BaseVehicle> vehicles) {
        for (int _int = 0; _int < this.capacity; _int++) {
            if (this.idToVehicle[_int] != null) {
                vehicles.add(this.idToVehicle[_int]);
            }
        }
    }

    public void Reset() {
        Arrays.fill(this.idToVehicle, null);
        this.freeIDSize = (short)this.capacity;
        short _short = 0;

        while (_short < this.capacity) {
            this.freeID[_short] = _short++;
        }
    }

    public short allocateID() {
        if (GameClient.bClient) {
            throw new RuntimeException("client must not call this");
        } else if (this.freeIDSize > 0) {
            return this.freeID[--this.freeIDSize];
        } else if (this.capacity >= 32767) {
            if (this.warnCount < 100) {
                DebugLog.log("warning: ran out of unique vehicle ids");
                this.warnCount++;
            }

            return -1;
        } else {
            this.resize(this.capacity + 256);
            return this.allocateID();
        }
    }

    private void resize(int int1) {
        int int0 = this.capacity;
        this.capacity = Math.min(int1, 32767);
        this.capacity = Math.min(int1, 32767);
        this.idToVehicle = (BaseVehicle[])Arrays.copyOf(this.idToVehicle, this.capacity);
        this.freeID = Arrays.copyOf(this.freeID, this.capacity);

        for (int int2 = int0; int2 < this.capacity; int2++) {
            this.freeID[this.freeIDSize++] = (short)int2;
        }
    }
}
